package org.linlinjava.litemall.db.config;

import java.sql.SQLException;

import org.beetl.sql.core.mapping.type.JavaSqlTypeHandler;
import org.beetl.sql.core.mapping.type.TypeParameter;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ItextTypeHandler extends JavaSqlTypeHandler {
	private static final ObjectMapper mapper = new ObjectMapper();
	@Override
	public Object getValue(TypeParameter typePara) throws SQLException {
		Object obj = typePara.getObject();
		if(obj != null) {
			try {
				
				Class<?> target = typePara.getTarget();
				if( target.getComponentType() == JsonNode.class) {
					return mapper.readTree(obj.toString());
				}if( target.getComponentType() == String.class) {
					return mapper.readValue(obj.toString(), String[].class);
				}if( target.getComponentType() == Integer.class) {
					return mapper.readValue(obj.toString(), Integer[].class);
				}else {
					return mapper.readValue(obj.toString(), String[].class);
				}
				
			} catch (Exception e) {
				 throw new RuntimeException(e);
			} 
		}
		return null;
	}

}
